package com.xms.queryview.service;

import com.xms.core.components.QueryColumnSortInfo;
import com.xms.queryview.core.component.CellDescriptor;
import com.xms.queryview.core.component.GridDescriptor;
import com.xms.queryview.core.component.RowDescriptor;
import com.xms.queryview.entity.QueryView;
import com.xms.schema.core.EntityMask;
import com.xms.schema.entity.Attribute;
import com.xms.schema.entity.Entity;
import com.xms.sdk.core.query.OrderType;
import com.xms.sdk.core.query.QueryExpression;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * DefaultQueryViewProvider
 * </p>
 *
 * @author migo
 * @since 2021/9/6
 */
@Service
public class DefaultQueryViewProvider implements IDefaultQueryViewProvider {

    @Override
    public QueryView get(Entity entity, List<Attribute> attributes) {
        if (entity == null || CollectionUtil.isEmpty(attributes)
        || !attributes.stream().anyMatch(x->x.getName().equalsIgnoreCase("name")
                || x.getName().equalsIgnoreCase("createdon"))) {
            return null;
        }
        QueryView view = new QueryView();
        view.setName(entity.getLocalizedname());
        view.setEntityid(entity.getEntityid());
        view.setEntityname(entity.getName());
        view.setIsdefault(true);
        view.setStatecode(true);
        view.setIsprivate(false);
        view.setAuthorizationenabled(false);
        view.setIssimplefilter(false);
        view.setIscustombutton(false);
        view.setCreatedby(entity.getCreatedby());
        view.setCreatedon(LocalDateTime.now());
        view.setQueryviewid(UUIDUtil.get());
        //fetch
        QueryExpression _queryExpression = new QueryExpression(entity.getName());
        _queryExpression.AddOrder("createdon", OrderType.Descending);
        _queryExpression.ColumnSet.addColumns("name", "createdon");
        if (entity.getEntitymask() == EntityMask.User.value()) {
            _queryExpression.ColumnSet.addColumn("ownerid");
        }
        view.setFetchconfig(JsonUtil.toJSONString(_queryExpression));
        //layout
        GridDescriptor grid = new GridDescriptor();
        RowDescriptor row = new RowDescriptor();
        CellDescriptor nameCell = new CellDescriptor();
        nameCell.Name = "name";//primaryField.getName();
        nameCell.EntityName = entity.getName();
        nameCell.IsHidden = false;
        nameCell.IsSortable = true;
        nameCell.Width = 150;
        nameCell.Label = "";
        row.addCell(nameCell);
        CellDescriptor createdOnCell = new CellDescriptor();
        createdOnCell.Name = "createdon";
        createdOnCell.EntityName = entity.getName();
        createdOnCell.IsHidden = false;
        createdOnCell.IsSortable = true;
        createdOnCell.Width = 150;
        createdOnCell.Label = "";
        row.addCell(createdOnCell);
        if (entity.getEntitymask() == EntityMask.User.value()) {
            CellDescriptor ownerCell = new CellDescriptor();
            ownerCell.Name = "ownerid";
            ownerCell.EntityName = entity.getName();
            ownerCell.IsHidden = false;
            ownerCell.IsSortable = true;
            ownerCell.Width = 150;
            ownerCell.Label = "";
            row.addCell(ownerCell);
        }
        grid.Rows = new ArrayList<>();
        grid.addRow(row);
        grid.SortColumns = new ArrayList<>();
        grid.addSort(new QueryColumnSortInfo("createdon", false));
        view.setLayoutconfig(JsonUtil.toJSONString(grid));
        return view;
    }
}
